<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Entity\Phongban;
use Acme\DemoBundle\Entity\Nhom;
use Symfony\Component\HttpFoundation\Response;

class PhongBanController extends Controller
{
	
	private function getlist ($index,$thongbao) {
		$listPhongBan =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:Phongban')
		->findByCoditions(10*($index-1),10);
			
		$countPhongBan = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Phongban')->countAll();
			
		$count = \Count($countPhongBan)/10;
			
		$mod = \Count($countPhongBan)%10;
			
		if($mod>0){
			$count++;
		}
		
		return $this->render(
				'AcmeDemoBundle:Demo:phongban_list.html.twig',
				array('listPhongBan' => $listPhongBan,
						'index' => $index,
						'count' => $count,
						'thongbao' => $thongbao
				)
		);
	}
	
	public function getNhomAction()
	{
		$lstNhom = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Nhom')
		->getAll();
		$count = 0;
		$result = "";
		$result .= '{"total":'.\Count($lstNhom).',"rows":[';
		foreach ($lstNhom as $nhom) {
			$count++;
			if($count>= \Count($lstNhom)) {
				$result .= '{"id":"'. $nhom->getMa() .'","name":"'. $nhom->getTen() .'"}';
			}else {
				$result .= '{"id":"'. $nhom->getMa() .'","name":"'. $nhom->getTen() .'"},';
			}
		}
		
		$result .= ']}';
		
		$response = new Response($result);
		$response->headers->set('Content-Type', 'application/json');
		
		return $response;
	}
	
	public function inputAction()
	{
		$lstNhom = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Nhom')
		->getAll();
		$request = $this->get('request');
		if ($request->isMethod('POST')) {
			$ma = $request->request->get('ma');
			$ten = $request->request->get('ten');
			$diachi = $request->request->get('diachi');
			$manhom = $request->request->get('nhom');
			
			
			$nhom = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:Nhom')
			->findOneByMa($manhom);
			
			if(!$nhom){
				return $this->render(
						'AcmeDemoBundle:Demo:phongban_input.html.twig',
						array('exist' => "notok",
								'ma' => $ma,
								'ten' => $ten,
								'diachi' => $diachi,
								'manhom' => $manhom,
								'lstNhom' => $lstNhom
						)
				);
			}
			
			$check = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:Phongban')
			->checkMaInsert($ma);
				
			if($check){
				return $this->render(
						'AcmeDemoBundle:Demo:phongban_input.html.twig',
						array('exist' => "ok",
								'ma' => $ma,
								'ten' => $ten,
								'diachi' => $diachi,
								'manhom' => $manhom
						)
				);
			}
			
			
			
			$phongban = new Phongban();
			$phongban->setMa($ma);
			$phongban->setTen($ten);
			$phongban->setDiachi($diachi);
			$phongban->setCreated(new \DateTime('now'));
			$phongban->setNhomid($nhom);
			$this->getDoctrine()
			->getRepository('AcmeDemoBundle:Phongban')
			->savePhongBan($phongban);


			return $this->getlist(1,"ok");

		}
		
		
		
		return $this->render(
				'AcmeDemoBundle:Demo:phongban_input.html.twig',
						array('exist' => null,
								'ma' => "",
								'ten' => "",
								'diachi' => "",
								'manhom' => ""
						)
		);
	}
	public function listAction($index)
	{
		return $this->getlist($index,null);
		
	}
	public function detailAction($id)
	{
	
		$phongban = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Phongban')
		->find($id);
		 
		return $this->render(
				'AcmeDemoBundle:Demo:phongban_edit.html.twig',
				array('phongban' => $phongban,'exist' => null,'idnhom' => strval( $phongban->getNhomid()->getMa()))
		);
	}
	public function editAction($id)
	{
		
		
		$phongban = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Phongban')
		->find($id);
		$request = $this->get('request');

		$ma = $request->request->get('ma');
		$ten = $request->request->get('ten');
		$diachi = $request->request->get('diachi');

		$manhom = $request->request->get('nhom');
			
		$nhom = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Nhom')
		->findOneByMa($manhom);
		
		if(!$nhom){
			return $this->render(
					'AcmeDemoBundle:Demo:phongban_edit.html.twig',
					array('phongban' => $phongban,'exist' => 'notok','idnhom' => $phongban->getNhomid()->getMa()
							
					)
			);
		}
		
		if($phongban->getMa() != $ma) {
			$Check = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:Phongban')
			->checkMaUpdate($ma,$phongban->getMa());
			if($Check){
				return $this->render(
						'AcmeDemoBundle:Demo:phongban_edit.html.twig',
						array('phongban' => $phongban,'exist' => 'ok','idnhom' => $phongban->getNhomid()->getMa()
								)
				);
			}
		}

		
		
		$phongban->setMa($ma);
		$phongban->setTen($ten);
		$phongban->setDiachi($diachi);
		$phongban->setUpdated(new \DateTime('now'));
		$phongban->setNhomid($nhom);
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:Phongban')
		->savePhongBan($phongban);

		return $this->getlist(1,"ok");


	}
	public function delAction($id)
	{
		$phongban = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Phongban')
		->find($id);
		
		$phongban->setDeleted(new \DateTime('now'));
		
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:Phongban')
		->savePhongBan($phongban);

		return $this->getlist(1,"ok");
		
	}

}
